package com.letu.annotation.base;

import com.letu.annotation.annotation.Contants.CountryIdContant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.stream.Stream;

/**
 * <p>
 * 语言环境枚举类
 *
 * </p>
 *
 * @author zjn
 * @date 2021-07-29
 */
@Getter
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "LanguageTypeEnum", description = "语言 #{CN:中文;EN:英文;ALL:全部-枚举")
public enum LanguageTypeEnum implements BaseEnum {

    /**
     * CN="中文"
     */
    CN("中文"),
    /**
     * EN="英文"
     */
    EN("英文"),
    /**
     * ALL="全部"
     */
    ALL("全部"),
    ;

    @ApiModelProperty(value = "描述")
    private String desc;


    /**
     * 根据当前枚举的name匹配
     */
    public static LanguageTypeEnum match(String val, LanguageTypeEnum def) {
        return Stream.of(values()).parallel().filter(item -> item.name().equalsIgnoreCase(val)).findAny().orElse(def);
    }

    public static LanguageTypeEnum get(String val) {
        return match(val, null);
    }

    public boolean eq(LanguageTypeEnum val) {
        return val != null && eq(val.name());
    }

    @Override
    @ApiModelProperty(value = "编码", allowableValues = "CN,EN", example = "CN")
    public String getCode() {
        return this.name();
    }

    public static LanguageTypeEnum parseByCountry(Integer countryId) {
        if (countryId == null) {
            return LanguageTypeEnum.CN;
        }
        if (!CountryIdContant.CHINA.equals(countryId)) {
            return LanguageTypeEnum.EN;
        }
        return LanguageTypeEnum.CN;
    }

    public static LanguageTypeEnum parse(String language) {
        if (null == language || language.length() == 0) {
            return CN;
        } else if ("en".equals(language)
                || "EN".equals(language)
                || "us".equals(language)
                || "US".equals(language)
                || "uk".equals(language)
                || "UK".equals(language)
                || "English".equals(language)
                || "ENGLISH".equals(language)) {
            return EN;
        } else if ("all".equals(language)
                || "ALL".equals(language)) {
            return ALL;
        } else {
            return CN;
        }
    }
}
